using System;
using System.Collections.Generic;
using System.Text;
using SlimDX;
using System.Xml;
using Barrage.Global;
using Barrage.GUI.Elements;
using Barrage.Textures;

namespace Barrage.GUI.Templates.Standard
{
    struct StandardGUICursorAttributes
    {
        const string XML_TEXTURE_ATTRIBUTE = "texture";
        const string XML_WIDTH_ATTRIBUTE = "width";
        const string XML_HEIGHT_ATTRIBUTE = "height";
        const string XML_HOTSPOTX_ATTRIBUTE = "hotSpotX";
        const string XML_HOTSPOTY_ATTRIBUTE = "hotSpotY";


        public string Texture;
        public Vector2 Size;
        public Vector2 HotSpot;

        const string DEFAULT_TEXTURE = "Standard/cursor.png";
        const float DEFAULT_WIDTH = 24;
        const float DEFAULT_HEIGHT = 32;
        const float DEFAULT_HOTSPOTX = 0;
        const float DEFAULT_HOTSPOTY = 0;

        public static StandardGUICursorAttributes DefaultValues
        {
            get
            {
                return new StandardGUICursorAttributes(
                    Store.PathConfig[Store.TEMPLATE_PATH] + DEFAULT_TEXTURE,
                    new Vector2(DEFAULT_WIDTH, DEFAULT_HEIGHT),
                    new Vector2(DEFAULT_HOTSPOTX,DEFAULT_HOTSPOTY)
                    );
            }
        }

        public StandardGUICursorAttributes(string texture, Vector2 size,Vector2 hotSpot)
        {
            this.Texture = texture;
            this.Size = size;
            this.HotSpot = hotSpot;
        }

        public StandardGUICursorAttributes(XmlNode xn)
        {
            Texture = XmlHelper.GetXMLAttribute(xn, XML_TEXTURE_ATTRIBUTE, Store.PathConfig[Store.TEMPLATE_PATH] + DEFAULT_TEXTURE);
            Size = XmlHelper.ParseXMLVector2Attribute(xn,XML_WIDTH_ATTRIBUTE,XML_HEIGHT_ATTRIBUTE,new Vector2(DEFAULT_WIDTH, DEFAULT_HEIGHT));
            HotSpot = XmlHelper.ParseXMLVector2Attribute(xn, XML_HOTSPOTX_ATTRIBUTE, XML_HOTSPOTY_ATTRIBUTE, new Vector2(DEFAULT_HOTSPOTX, DEFAULT_HOTSPOTY));
        }
    }
    /// <summary>
    /// Standard template implementation of the GUICursor element
    /// </summary>
    public class StandardGUICursor : GUICursor
    {
        const string XML_DEFAULT_ATTRIBUTE = "default";
        StandardGUICursorAttributes attributes;
        
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="layout">Layout that will hold this item</param>
        /// <param name="xn">Xml Node that holds attribute values of this item</param>
        public StandardGUICursor(GUILayout layout, XmlNode xn)
            : base(layout, GUITemplateTypes.Standard, xn)
        {
            processDefaultValues(xn);

            addMesh(new GUI.Meshes.SimpleRectangle(TextureManager.Textures[attributes.Texture], attributes.Size));

        }

        void processDefaultValues(XmlNode xn)
        {
            bool loadDefaults = XmlHelper.GetXMLAttribute(xn, XML_DEFAULT_ATTRIBUTE).Equals("true");
            if (loadDefaults)
                attributes = StandardGUICursorAttributes.DefaultValues;
            else
                attributes = new StandardGUICursorAttributes(xn);

        }

        /// <summary>
        /// \internal Updates item matrices
        /// </summary>
        protected override void DoUpdate()
        {
            base.DoUpdate();
            motion.Translation += new Vector3(attributes.Size.X / 2f - attributes.HotSpot.X, attributes.HotSpot.Y - attributes.Size.Y / 2f, 0);
        }

    }
}
